import string

from flask import Flask
from flask import request
from flask import render_template
import func
from flask import jsonify  # json 转 dict
from jieba.analyse import extract_tags


app = Flask(__name__)  # 创建一个Flash实例


@app.route("/")
def main():
    return render_template("main.html")


@app.route("/time")
def get_top2_time():
    return func.get_top2_time()


@app.route("/mid1")
def get_mid1_data():
    data = func.get_mid1_data()
    dic = {"confiirm": data[0], "suspect": data[1], "heal": data[2], "dead": data[3]}
    return jsonify(dic)


@app.route("/mid2")
def get_mid2_data():
    res = []
    for tup in func.get_mid2_data():
        dic = {"name": tup[0], "value": int(tup[1])}
        res.append(dic)
    print(res)
    return jsonify({"data": res})

@app.route("/left1")
def get_left1_data():
    data = func.get_left1_data()
    day, confirm, suspect, heal, dead = [], [], [], [], []
    for d in data:
        day.append(d[0].strftime("%m-%d"))
        confirm.append(d[1])
        suspect.append(d[2])
        heal.append(d[3])
        dead.append(d[4])
    dic = {"day": day, "confirm": confirm, "suspect": suspect, "heal": heal, "dead": dead}
    print(dic)
    return jsonify(dic)

@app.route("/left2")
def get_left2_data():
    data = func.get_left2_data()
    day, confirm_add, suspect_add, heal_add, dead_add = [], [], [], [], []
    for d in data:
        day.append(d[0].strftime("%m-%d"))

        confirm_add.append(d[1])
        suspect_add.append(d[2])
        heal_add.append(d[3])
        dead_add.append(d[4])

    dic = {"day": day, "confirm_add": confirm_add, "suspect_add": suspect_add, "heal_add": heal_add, "dead_add": dead_add}
    print(dic)
    return jsonify(dic)

@app.route("/right1")
def get_right1_data():
    data = func.get_right1_data()
    city = []
    confirm = []

    for d in data:
        city.append(d[0])
        confirm.append(int(d[1]))

    dic = {"city": city, "confirm": confirm}
    # print(dic)
    return jsonify(dic)

@app.route("/right2")
def get_right2_data():
    data = func.get_right2_data()  # (('航天员太空过年吃啥馅饺子?', 4962929), ('南北方将迎下半年来最冷清晨', 4829320),....,)
    d = []
    for i in data:
        k, v = i[0], i[1]
        ks = extract_tags(k)  # 使用jieba提取关键字

        for j in ks:
            if not j.isdigit():
                d.append({"name": j, "value": str(v)})
    # print(d)
    return jsonify({"kws": d})






# @app.route('/check')
# def check():
#     id = request.values.get("id")
#     return f"""
#     <form action="/login">
#         账号: <input name="name" value="{id}"></br>
#         密码: <input name="passwd" value="{id}"></br>
#         <input type="submit">
#     </form>
#     """
#
#
# @app.route('/login')
# def login():
#     name = request.values.get("name")
#     passwd = request.values.get("passwd")
#     return f"name = {name}, passwd={passwd}"
#
#
# @app.route('/index')
# def index():
#     return render_template("index.html")  # 返回一个模板
#
#
# @app.route('/ajax', methods=["get", "post"])
# def ajax():
#     return "12580"


if __name__ == '__main__':
    app.run()
